#include<bits/stdc++.h>
using namespace std;
#define int long long

struct node {
	int x, y, step;
};

int n;
int qx, qy, zx, zy;
char ch[1005][1005];
bool vis[1005][1005];

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

void bfs() {
	queue<node> q;
	q.push({qx, qy, 0});
	vis[qx][qy] = 1;

	while (!q.empty()) {
		node t = q.front();
		q.pop();

		if (t.x == zx && t.y == zy){
			cout << t.step;
			return ;
		}
		
		for (int i = 0; i < 4; i++) {
			int xx = t.x + dx[i];
			int yy = t.y + dy[i];

			if (vis[xx][yy] || ch[xx][yy] != '0')
				continue;

			q.push({xx, yy, t.step + 1});
			vis[xx][yy] = 1;
		}
	}
}

signed main() {
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> ch[i][j];

	cin >> qx >> qy >> zx >> zy;
	bfs();
	
	return 0;
}
